//
//  WBHomeViewCell.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/16.
//  Copyright © 2016年 木木. All rights reserved.
//

import UIKit
import SDWebImage

private let margin :CGFloat = 8
private let minMargin:CGFloat = 2
private let W = (USC_width - 2 * margin - 2*minMargin) / 3

class WBHomeViewCell: UITableViewCell {

    
    //头像
    @IBOutlet weak var imageIcon: UIImageView!
    //微博姓名
    @IBOutlet weak var lblName: UILabel!
    //等级
    @IBOutlet weak var imageMemberShip: UIImageView!
    //时间
    @IBOutlet weak var lblTime: UILabel!
    //来源
    @IBOutlet weak var lblSrouce: UILabel!
    //内容
    @IBOutlet weak var lblContent: UILabel!
    //认证
    @IBOutlet weak var imageAvatar: UIImageView!
    //collectionView
    @IBOutlet weak var collectionView: WBPictureView!
    //flowLayout
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    //图片宽度
    @IBOutlet weak var itemWidth: NSLayoutConstraint!
     //图片高度
    @IBOutlet weak var itemHight: NSLayoutConstraint!
    //转发的标题文字(间距)
    @IBOutlet weak var lblDistance: NSLayoutConstraint!
    //评论数
    @IBOutlet weak var btnCommentCount: UIButton!
   //转发数
    @IBOutlet weak var BtnReposts: UIButton!
    //点赞数
    @IBOutlet weak var btnAttitudes: UIButton!

    //转发的微博
    @IBOutlet weak var lblRetweetedContent: UILabel!
  //底部的toolbar
    @IBOutlet weak var toolbar: UIToolbar!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        //对lblContext 设置
        lblContent.numberOfLines = 0;
        lblContent.preferredMaxLayoutWidth =  USC_width - 2*margin
        
        //流水布局 设置item
        flowLayout.itemSize = CGSize(width: W, height: W)
        flowLayout.minimumLineSpacing = minMargin
        flowLayout.minimumInteritemSpacing = minMargin
        
        self.lblRetweetedContent?.numberOfLines = 0;
        self.lblRetweetedContent?.preferredMaxLayoutWidth = USC_width - 2*margin
        
        layoutIfNeeded()
        
        //选中样式
        self.selectionStyle = .none
        
    }
    
    
    var viewModel :WBStatuesViewModel?{
        didSet{
            lblTime.text = viewModel?.timeText
            lblSrouce.text = viewModel?.soruceText
            lblContent.text = viewModel?.viewmodel?.text
            imageIcon.sd_setImage(with: viewModel?.urlString)
            
            imageMemberShip.image = viewModel?.mRankImage
            lblName.text = viewModel?.viewmodel?.user?.name
            imageAvatar.image = viewModel?.verifitedImage
            
            let count = viewModel?.urlStringS?.count ?? 0
            lblDistance?.constant = (count == 0 ? 0 : margin)
            
            let itemSize = calculatePaictureViewSize(count:count)
            itemWidth.constant = itemSize.pSize.width
            itemHight.constant = itemSize.pSize.height
            
            //MARK:
            // MARK: - //让一张图片填充完成
            flowLayout.itemSize = itemSize.itemSize
            collectionView.pictureInfoS = viewModel?.urlStringS
            
            lblRetweetedContent?.text = viewModel?.viewmodel?.retweeted_status?.text
         
            BtnReposts.setTitle("\(viewModel?.viewmodel?.reposts_count ?? 0)", for: .normal)
            btnCommentCount.setTitle("\(viewModel?.viewmodel?.comments_count ?? 0)", for: .normal)
            btnAttitudes.setTitle("\(viewModel?.viewmodel?.attitudes_count ?? 0)", for: .normal)
            
            collectionView.layoutIfNeeded()
            
        }
    }
    
    //根据图片张数计算配图视图的大小,更新配图视图的宽高
    //元组类型:可以使用多个类型合成一个类型
    private func calculatePaictureViewSize(count:Int)->(pSize:CGSize,itemSize:CGSize){
        if count == 0 {
            return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
        }
        if count == 1{
            
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.urlStringS?.last?.wap360 ?? "") {
                let  imageSize  = image.size
                
                return(imageSize,imageSize)
            }
            return(CGSize(width: W, height: W),CGSize(width: W, height: W))
        }
        
        //判断4张图片
        if count == 4 {
            let width = 2 * W + margin
            return (CGSize(width: width, height: width),CGSize(width: W, height: W))
        }
        
        //其他多张图
         let  cow = 3;
        let row = (count - 1) / cow + 1
        let H =  CGFloat(row)*W  +  (CGFloat(row)-1)*minMargin
        
        return (CGSize(width: USC_width - 2*margin, height:H),CGSize(width: W, height: W))
        
    }

}
    
    //MARK:  -
    //MARK:  - 对collectionView 进行布局
    
//    func calculateContentSize(count:Int) ->CGSize{
//        
//        if count == 0 {
//            return CGSize(width: 0, height: 0)
//        }
//        
//        if count == 4 {
//            
//            return CGSize(width: W*2 + margin , height: W*2 + margin)
//        }
//        
//        //对  1,2,3  
////              4,5,6
//        let  cow = 3;
//        let row = (count - 1) / cow + 1
//        let H =  CGFloat(row)*W  +  (CGFloat(row)-1)*minMargin
// 
//        return CGSize(width: USC_width - 2*margin, height:H)
//    }
//    
//    

